// J2ME GPS Track
// Copyright (C) 2006 Dana Peters
// http://www.qcontinuum.org/gpstrack

package uns.fit.gis.gps;

import java.io.*;
import javax.microedition.rms.*;

public class Preferences {

    final static int CONNECTTYPE_NONE = 0;
    final static int CONNECTTYPE_BLUETOOTH = 1;
    final static int CONNECTTYPE_SERIAL = 2;
    
    final static int UNITTYPE_METRIC = 0;
    final static int UNITTYPE_US = 1;
    
    final static private String RECORDSTORENAME = "preferences";
    
    private int mConnectType, mUnitType, mBacklightSeconds;
    private int mMinDistance, mMinTime;
    private String mBluetoothHost, mBluetoothUrl;
    private String mPort, mBaud;
    private String mObexHost, mObexUrl;
    private String mEmailAddress;
    private String mUserName;
    
    public Preferences() {
        load();
    }

    public int getUnitType() {
        return mUnitType;
    }
    
    public void setUnitType(int unitType) {
        mUnitType = unitType;
    }
    
    public int getBacklightSeconds() {
        return mBacklightSeconds;
    }
    
    public void setBacklightSeconds(int backlightSeconds) {
        mBacklightSeconds = backlightSeconds;
    }
    
    public int getConnectType() {
        return mConnectType;
    }

    public void setConnectType(int connectType) {
        mConnectType = connectType;
    }
 
    public int getMinDistance() {
        return mMinDistance;
    }
    
    public void setMinDistance(int minDistance) {
        mMinDistance = minDistance;
    }
    
    public int getMinTime() {
        return mMinTime;
    }
    
    public void setMinTime(int minTime) {
        mMinTime = minTime;
    }

    public String getBluetoothHost() {
        return mBluetoothHost;
    }
    
    public void setBluetoothHost(String bluetoothHost) {
        mBluetoothHost = bluetoothHost;
    }

    public String getBluetoothUrl() {
        return mBluetoothUrl;
    }
    
    public void setBluetoothUrl(String bluetoothUrl) {
        mBluetoothUrl = bluetoothUrl;
    }

    public String getPort() {
        return mPort;
    }
    
    public void setPort(String port) {
        mPort = port;
    }
    
    public String getBaud() {
        return mBaud;
    }
    
    public void setBaud(String baud) {
        mBaud = baud;
    }
    
    public String getObexHost() {
        return mObexHost;
    }
    
    public void setObexHost(String obexHost) {
        mObexHost = obexHost;
    }
    
    public String getObexUrl() {
        return mObexUrl;
    }
    
    public void setObexUrl(String obexUrl) {
        mObexUrl = obexUrl;
    }
    
    public String getEmailAddress() {
        return mEmailAddress;
    }
    
    public void setEmailAddress(String emailAddress) {
        mEmailAddress = emailAddress;
    }

    public String getUserName() {
        return mUserName;
    }
    
    public void setUserName(String userName) {
        mUserName = userName;
    }

    public void save() {
    	
        RecordStore recordStore;
        try {
            recordStore = RecordStore.openRecordStore(RECORDSTORENAME, true);
        }
        catch (RecordStoreException rsc) {
            return;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream outputStream = new DataOutputStream(baos);
        try {
            outputStream.writeInt(mUnitType);
            outputStream.writeInt(mBacklightSeconds);
            outputStream.writeInt(mConnectType);
            outputStream.writeInt(mMinDistance);
            outputStream.writeInt(mMinTime);
            outputStream.writeUTF(removeNull(mBluetoothHost));
            outputStream.writeUTF(removeNull(mBluetoothUrl));
            outputStream.writeUTF(removeNull(mPort));
            outputStream.writeUTF(removeNull(mBaud));
            outputStream.writeUTF(removeNull(mObexHost));
            outputStream.writeUTF(removeNull(mObexUrl));
            outputStream.writeUTF(removeNull(mEmailAddress));
            outputStream.writeUTF(removeNull(mUserName));
            byte[] bytes = baos.toByteArray();
            if (recordStore.getNumRecords() == 0)
                recordStore.addRecord(bytes, 0, bytes.length);
            else
                recordStore.setRecord(1, bytes, 0, bytes.length);
            outputStream.close();
            recordStore.closeRecordStore();
        }
        catch (Exception exception) {
        }
        
    }
    
    private String removeNull(String text) {
        return text != null ? text : "";
    }
    
    private String restoreNull(String text) {
        return text.length() > 0 ? text : null;
    }
    
    private void load() {
    	
        RecordStore recordStore;
        try {
	    recordStore = RecordStore.openRecordStore(RECORDSTORENAME, false);
            byte[] bytes = recordStore.getRecord(1);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            DataInputStream inputStream = new DataInputStream(bais);
            mUnitType = inputStream.readInt();
            mBacklightSeconds = inputStream.readInt();
            mConnectType = inputStream.readInt();
            mMinDistance = inputStream.readInt();
            mMinTime = inputStream.readInt();
            mBluetoothHost = restoreNull(inputStream.readUTF());
            mBluetoothUrl =  restoreNull(inputStream.readUTF());
            mPort =  restoreNull(inputStream.readUTF());
            mBaud = restoreNull(inputStream.readUTF());
            mObexHost =  restoreNull(inputStream.readUTF());
            mObexUrl =  restoreNull(inputStream.readUTF());
            mEmailAddress = restoreNull(inputStream.readUTF());
            mUserName = restoreNull(inputStream.readUTF());
            inputStream.close();
            recordStore.closeRecordStore();
        }
        catch (Exception exception) {
            mUnitType = UNITTYPE_METRIC;
            mBacklightSeconds = 0;
            mConnectType = CONNECTTYPE_NONE;
            mMinDistance = 10;
            mMinTime = 10;
            mBluetoothHost = null;
            mBluetoothUrl = null;
            mPort = null;
            mBaud = null;
            mObexHost = null;
            mObexUrl = null;
            mEmailAddress = "";
            mUserName = "guest";
            save();
            return;
        }
        
    }
}
